package com.facebook.drawee.view;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.ColorStateList;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Build;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.facebook.common.internal.ToStringHelper;
import com.facebook.common.internal.Preconditions;
import com.facebook.drawee.interfaces.DraweeController;
import com.facebook.drawee.interfaces.DraweeHierarchy;

import javax.annotation.Nullable;

public class DraweeView<DH extends DraweeHierarchy> extends ImageView {
    private final Spec mMeasureSpec;
    private float mAspectRatio;
    private DraweeHolder<DH> mDraweeHolder;
    private boolean mInitialised;

    public DraweeView(Context context) {
        super(context);
        this.mMeasureSpec = new Spec();
        this.mAspectRatio = 0f;
        this.mInitialised = false;
        this.init();
    }

    public DraweeView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mMeasureSpec = new Spec();
        this.mAspectRatio = 0f;
        this.mInitialised = false;
        this.init();
    }

    public DraweeView(Context context,
                      AttributeSet attrs,
                      int defaultStyle) {
        super(context, attrs, defaultStyle);
        this.mMeasureSpec = new Spec();
        this.mAspectRatio = 0f;
        this.mInitialised = false;
        this.init();
    }

    @TargetApi(value = 21)
    public DraweeView(Context context, AttributeSet attrs, int defaultStyle, int arg5) {
        super(context, attrs, defaultStyle, arg5);
        this.mMeasureSpec = new Spec();
        this.mAspectRatio = 0f;
        this.mInitialised = false;
        this.init();
    }

    private void init() {
        if (!this.mInitialised) {
            this.mInitialised = true;
            this.mDraweeHolder = new DraweeHolder();
            if (Build.VERSION.SDK_INT >= 21) {
                ColorStateList colorStateList = this.getImageTintList();
                if (colorStateList != null) {
                    this.setColorFilter(colorStateList.getDefaultColor());
                }
            }
        }
    }

    public float getAspectRatio() {
        return this.mAspectRatio;
    }

    @Nullable
    public DraweeController getController() {
        return this.mDraweeHolder.mController;
    }

    public DraweeHierarchy getHierarchy() {
        return Preconditions.checkNotNull(this.mDraweeHolder.mHierarchy);
    }

    @Nullable
    public Drawable getTopLevelDrawable() {
        return this.mDraweeHolder.getTopLevelDrawable();
    }

    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        this.mDraweeHolder.trim();
    }

    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        this.mDraweeHolder.untrim();
    }

    public void onFinishTemporaryDetach() {
        super.onFinishTemporaryDetach();
        this.mDraweeHolder.trim();
    }

    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        this.mMeasureSpec.width = widthMeasureSpec;
        this.mMeasureSpec.height = heightMeasureSpec;
        ViewGroup.LayoutParams layoutParams = this.getLayoutParams();
        int paddingHorizontal = this.getPaddingLeft() + this.getPaddingRight();
        int paddingVertical = this.getPaddingTop() + this.getPaddingBottom();
        if (mAspectRatio > 0f && layoutParams != null) {
            if (shouldAdjust(layoutParams.height)) {
                mMeasureSpec.height = View.MeasureSpec.makeMeasureSpec(
                        View.resolveSize((int) ((float) (MeasureSpec.getSize(mMeasureSpec.width) - paddingHorizontal) / mAspectRatio + (float) paddingVertical), mMeasureSpec.height),
                        MeasureSpec.EXACTLY);
            } else if (shouldAdjust(layoutParams.width)) {
                mMeasureSpec.width = View.MeasureSpec.makeMeasureSpec(
                        View.resolveSize((int) (mAspectRatio * (float) (MeasureSpec.getSize(mMeasureSpec.height) - paddingVertical) + (float) paddingHorizontal), mMeasureSpec.width),
                        MeasureSpec.EXACTLY);
            }
        }
        super.onMeasure(this.mMeasureSpec.width, this.mMeasureSpec.height);
    }

    public void onStartTemporaryDetach() {
        super.onStartTemporaryDetach();
        this.mDraweeHolder.untrim();
    }

    public boolean onTouchEvent(MotionEvent motionEvent) {
        return mDraweeHolder.mController != null
                && (mDraweeHolder.mController.onTouchEvent(motionEvent)
                || super.onTouchEvent(motionEvent));
    }

    public void setAspectRatio(float aspectRatio) {
        if (aspectRatio != this.mAspectRatio) {
            this.mAspectRatio = aspectRatio;
            this.requestLayout();
        }
    }

    public void setController(@Nullable DraweeController draweeController) {
        this.mDraweeHolder.setController(draweeController);
        super.setImageDrawable(this.mDraweeHolder.getTopLevelDrawable());
    }

    public void setHierarchy(DH dh) {
        this.mDraweeHolder.setHierarchy(dh);
        super.setImageDrawable(this.mDraweeHolder.getTopLevelDrawable());
    }

    @Deprecated
    public void setImageBitmap(Bitmap bitmap) {
        this.getContext();
        this.init();
        this.mDraweeHolder.setController(null);
        super.setImageBitmap(bitmap);
    }

    @Deprecated
    public void setImageDrawable(Drawable drawable) {
        this.getContext();
        this.init();
        this.mDraweeHolder.setController(null);
        super.setImageDrawable(drawable);
    }

    @Deprecated
    public void setImageResource(int resId) {
        this.getContext();
        this.init();
        this.mDraweeHolder.setController(null);
        super.setImageResource(resId);
    }

    @Deprecated
    public void setImageURI(Uri uri) {
        this.getContext();
        this.init();
        this.mDraweeHolder.setController(null);
        super.setImageURI(uri);
    }

    public String toString() {
        ToStringHelper toStringHelper = android.support.design.a.toStringHelper(this);
        return toStringHelper.add("holder",
                this.mDraweeHolder != null
                        ? this.mDraweeHolder.toString()
                        : "<no holder set>").toString();
    }

    public static boolean shouldAdjust(int height) {
        return height == 0 || height == -2;
    }
}

